package com.szd.qiongyou20.home.search.result_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.bean.HotSearchBean;
import com.szd.qiongyou20.home.bean.KeywordBus;
import com.szd.qiongyou20.home.presenter.SearchPre;
import com.szd.qiongyou20.home.view.SearchDataToView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * Created by SZD on 2016/12/27.
 */

public class Select_Fragment extends Fragment implements SearchDataToView {

    private Context mContext;
    private ListView mListView;
    private SearchPre mSearchPre;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();

        mSearchPre = new SearchPre(this);
        mSearchPre.loadDataToView(mContext);
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listview_search, null);
        mListView = (ListView) view.findViewById(R.id.listView_result);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void dataToView(HotSearchBean hotSearchBean) {
        final List<String> stringList = hotSearchBean.getData();
        Log.i("==========", "dataToView: "+stringList);
        ArrayAdapter arrayAdapter = new ArrayAdapter(mContext, R.layout.item_search_listview, R.id.textView_search_item, stringList);
        mListView.setAdapter(arrayAdapter);

        // listView上条目的点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String keyword = stringList.get(position);
                EventBus.getDefault().post(new KeywordBus(keyword));
            }
        });
    }
}
